package com.example.zht.wechat;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends FragmentActivity implements OnClickListener {
    // 底部菜单3个Linearlayout
    private LinearLayout ll_home;
    private LinearLayout ll_address;
    private LinearLayout ll_friend;
    // 底部菜单3个ImageView
    private ImageView iv_home;
    private ImageView iv_address;
    private ImageView iv_friend;
    // 底部菜单3个菜单标题
    private TextView tv_home;
    private TextView tv_address;
    private TextView tv_friend;
    // 3个页面
    private Fragment homeFragment;
    private Fragment addressFragment;
    private Fragment friendFragment;
    private long lastTime = 0; // 退出键的最后时间

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //当系统版本为 4.4 或者以上时可以使用沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明导航栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        // 初始化控件
        initView();
        // 初始化底部按钮事件
        initEvent();
        // 初始化并设置当前 Fragment
        initFragment(0);
    }

    private void initFragment(int index) {
        // 引用了V4包下的Fragment，这里的管理器用 getSupportFragmentManager() 获取
        FragmentManager fragmentManager = getSupportFragmentManager();
        // 开启事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 隐藏所有 Fragment
        hideFragment(transaction);
        switch (index) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.fl_content, homeFragment);
                } else {
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                if (addressFragment == null) {
                    addressFragment = new ContactFragment();
                    transaction.add(R.id.fl_content, addressFragment);
                } else {
                    transaction.show(addressFragment);
                }
                break;
            case 2:
                if (friendFragment == null) {
                    friendFragment = new FindFragment();
                    transaction.add(R.id.fl_content, friendFragment);
                } else {
                    transaction.show(friendFragment);
                }
                break;
            default:
                break;
        }
        // 提交事务
        transaction.commit();
    }

    // 隐藏Fragment
    private void hideFragment(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (addressFragment != null) {
            transaction.hide(addressFragment);
        }
        if (friendFragment != null) {
            transaction.hide(friendFragment);
        }
    }

    // 设置按钮监听
    private void initEvent() {
        ll_home.setOnClickListener(this);
        ll_address.setOnClickListener(this);
        ll_friend.setOnClickListener(this);
    }

    private void initView() {
        // 底部菜单3个Linearlayout
        this.ll_home = findViewById(R.id.ll_home);
        this.ll_address = findViewById(R.id.ll_address);
        this.ll_friend = findViewById(R.id.ll_friend);
        // 底部菜单3个ImageView
        this.iv_home = findViewById(R.id.iv_home);
        this.iv_address = findViewById(R.id.iv_address);
        this.iv_friend = findViewById(R.id.iv_friend);
        // 底部菜单3个菜单标题
        this.tv_home = findViewById(R.id.tv_home);
        this.tv_address = findViewById(R.id.tv_address);
        this.tv_friend = findViewById(R.id.tv_friend);
    }

    @Override
    public void onClick(View v) {
        // 在每次点击后将所有的底部按钮(ImageView, TextView) 颜色改为未选中模式，然后根据点击着色
        restartBotton();
        switch (v.getId()) {
            case R.id.ll_home:
                iv_home.setImageResource(R.drawable.tab_wechat_selected);
                tv_home.setTextColor(Color.parseColor("#4169E1"));
                initFragment(0);
                break;
            case R.id.ll_address:
                iv_address.setImageResource(R.drawable.tab_contacts_selected);
                tv_address.setTextColor(Color.parseColor("#4169E1"));
                initFragment(1);
                break;
            case R.id.ll_friend:
                iv_friend.setImageResource(R.drawable.tab_friend_selected);
                tv_friend.setTextColor(Color.parseColor("#4169E1"));
                initFragment(2);
                break;
            default:
                break;
        }

    }

    private void restartBotton() {
        iv_home.setImageResource(R.drawable.tab_wechat_normal);
        iv_address.setImageResource(R.drawable.tab_contacts_normal);
        iv_friend.setImageResource(R.drawable.tab_friend_normal);
        tv_home.setTextColor(Color.parseColor("#000000"));
        tv_address.setTextColor(Color.parseColor("#000000"));
        tv_friend.setTextColor(Color.parseColor("#000000"));
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - lastTime <= 2000) {
                finish();
            } else {
                Toast.makeText(this, "再按一次退出程序", Toast.LENGTH_SHORT).show();
                lastTime = System.currentTimeMillis();
            }
            return false;
        }
        return super.onKeyDown(keyCode, event);
    }
}